package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.HospInfoApplication;
import com.dhcc.DRGsHosp.core.domain.HospInfo;
import com.dhcc.DRGsHosp.facade.HospInfoFacade;
import com.dhcc.DRGsHosp.facade.dto.HospInfoDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.HospInfoAssembler;


@Named("drgsFacadeImplFulianxia20170118")
public class HospInfoFacadeImpl implements HospInfoFacade {

	@Autowired
	private HospInfoApplication  application;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public InvokeResult creatHospInfo(HospInfoDTO hospInfoDTO) {
		HospInfo hosp = application.getHospInfo(hospInfoDTO.getHospCode());
		if(null == hosp){
			application.creatHospInfo(HospInfoAssembler.toEntity(hospInfoDTO));
			return InvokeResult.success();
		}else{
			return InvokeResult.failure("此院区编码已存在！");
		}
	}
	
	public InvokeResult updateHospInfo(HospInfoDTO hospInfoDTO) {
		application.updateHospInfo(HospInfoAssembler.toEntity(hospInfoDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult removeHospInfos(String[] hospCodes) {
		Set<HospInfo> hosps= new HashSet<HospInfo>();
		for ( String hospCode : hospCodes) {
			HospInfo hosp = application.getHospInfo(hospCode);
			hosps.add(hosp);
		}
		application.removeHospInfos(hosps);
		return InvokeResult.success();
	}
	
	public Page<HospInfoDTO> pageQueryHospInfo(HospInfoDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _hospInfo from HospInfo _hospInfo   where 1=1 ");
	   	if (queryVo.getHospCode() != null && !"".equals(queryVo.getHospCode())) {
	   		jpql.append(" and _hospInfo.hospCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getHospCode()));
	   	}		
	   	if (queryVo.getHospName() != null && !"".equals(queryVo.getHospName())) {
	   		jpql.append(" and _hospInfo.hospName like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getHospName()));
	   	}		
	   	if (queryVo.getHospShortName() != null && !"".equals(queryVo.getHospShortName())) {
	   		jpql.append(" and _hospInfo.hospShortName like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getHospShortName()));
	   	}		
	   	if (queryVo.getHospLevel() != null && !"".equals(queryVo.getHospLevel())) {
	   		jpql.append(" and _hospInfo.hospLevel like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getHospLevel()));
	   	}		
	   	jpql.append(" order by _hospInfo.hospCode");
        Page<HospInfo> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<HospInfoDTO>(pages.getStart(), pages.getResultCount(),pageSize, HospInfoAssembler.toDTOs(pages.getData()));
	}

	/**
     *  
     *  isHospOrderExist.  
     *  @Description：院区编码是否存在
     *  @Author fulianxia
     *  @param hospOrder
     *  @return 
     *  @Date:2018年3月26日
     */
	@Override
	public boolean isHospOrderExist(String hospOrder,String hospCode) {
		return application.isHospOrderExist(hospOrder, hospCode)  ;
	}
	
}
